;+
; NAME:
;  hod_makeparfile
; PURPOSE:
;  Creates a parameter file for the HOD code. Specify parameters to
;  change from default
; CALLING SEQUENCE:
;  hod_makeparfile,parfile,[dir=,calc=,hodspec=,z=,cch1=,cch2=,rbin_lg=,$
;                           dmprof=,bmdl=,massfn=,conc=,coll_2h=,pimax=,$
;                           diag=,xim_meth=,inp_xim_rt=,sig_meth=,ip_dmh_rt=]
; INPUTS/OUTPUTS:
;  parfile - name of parameter file that will be written 
; 
; OPTIONAL KEYWORD INPUTS:
;  dir - specify directory for paramater file (dafault is
;        $HOD_DIR/parfiles/)
;  
; calc=,hodspec=,z=,cch1=,cch2=,rbin_lg=,$
; dmprof=,bmdl=,massfn=,conc=,coll_2h=,pimax=,$
; diag=,xim_meth=,inp_xim_rt=,sig_meth=,ip_dmh_rt=
; SEE CODE FOR DEFAULTS
;-
pro hod_makeparfile,parfile,dir=dir,calc=calc,hodspec=hodspec,z=z,cch1=cch1,cch2=cch2,rbin_lg=rbin_lg,rbin_ar=rbin_ar,$
                   dmprof=dmprof,bmdl=bmdl,massfn=massfn,conc=conc,coll_2h=coll_2h,pimax=pimax,$
                   diag=diag,xim_meth=xim_meth,inp_xim_rt=inp_xim_rt,sig_meth=sig_meth,ip_dmh_rt=ip_dmh_rt

if n_elements(parfile) eq 0 then begin
   splog,"Must specify parameter file"
   return
endif

if n_elements(dir) eq 0 then dir=getenv("HOD_DIR")+"/parfiles/"


;;; HOD default parameters (based on Zheng07 testrun from Miyaji
;if n_elements(calc) eq 0 then calc='wp xi'
;if n_elements(hodspec) eq 0 then hodspec='arg'
;if n_elements(z) eq 0 then z=0.92
;if n_elements(cch1) eq 0 then cch1='0.7,0.3,0.7'
;if n_elements(cch2) eq 0 then cch2='zheng07_lin_nl_delta2_z092.dat'
;if n_elements(dmprof) eq 0 then dmprof='nfw'
;if n_elements(bmdl) eq 0 then bmdl='TIN05'
;if n_elements(massfn) eq 0 then massfn='JEN01'
;if n_elements(conc) eq 0 then conc='ZCZ07'
;if n_elements(coll_2h) eq 0 then coll_2h='CS02'
;if n_elements(pimax) eq 0 then pimax=40
;;if n_elements(diag) eq 0 then diag='test1'
;if n_elements(xim_meth) eq 0 then xim_meth='calc'
;if n_elements(inp_dmh_rt) eq 0 then inp_dmh_rt='test1'


;;; HOD now defaults to the parameters that we actually want to use
if n_elements(calc) eq 0 then calc='wp xi'
if n_elements(hodspec) eq 0 then hodspec='arg'
if n_elements(z) eq 0 then z=0.92 ; N.B. this should be defined for each of our samples
if n_elements(cch1) eq 0 then cch1='0.7,0.25,0.75'
if n_elements(cch2) eq 0 then begin
    fz=string(floor(z),format='(I1)')
    dz=string((z-floor(z))*100,format='(I02)')
    cch2='zehavi11_lin_nl_delta2_z'+fz+dz+'.dat'
    if not file_test(getenv("HOD_DIR")+'/dat/HODdat/'+cch2) then $
      message,"File "+cch2+" does not exist. Need to run camb for this redshift"
endif
if n_elements(dmprof) eq 0 then dmprof='nfw'
if n_elements(bmdl) eq 0 then bmdl='TIN05'
if n_elements(massfn) eq 0 then massfn='JEN01'
if n_elements(conc) eq 0 then conc='ZCZ07'
if n_elements(coll_2h) eq 0 then coll_2h='CS02'
if n_elements(pimax) eq 0 then pimax=40
if n_elements(diag) eq 0 then diag='no'
if n_elements(xim_meth) eq 0 then xim_meth='calc'
if n_elements(inp_dmh_rt) eq 0 then inp_dmh_rt='test1' ; not used as xim_meth=calc



if n_elements(rbin_lg) ne 0 and n_elements(rbin_ar) ne 0 then begin
   splog,"ERROR: must specify rbin_lg OR rbin_ar (not both)"
   return
endif
if n_elements(rbin_lg) eq 0 and n_elements(rbin_ar) eq 0 then rbin_lg=[0.1,40,20] 




; Now writing the file
openw,lun,dir+parfile,/get_lun
splog,"Writing ",dir+parfile

printf,lun,'calc=',calc
printf,lun,'hodspec=',hodspec
printf,lun,'z=',strtrim(string(z),2)
printf,lun,'cch1=',cch1
printf,lun,'cch2=',cch2
if n_elements(rbin_lg) gt 0 then $
   printf,lun,'rbin_lg=',rbin_lg[0],',',rbin_lg[1],',',rbin_lg[2],format='(A8,F5.1,A1,F5.1,A1,I2)' $
else if n_elements(rbin_ar) gt 0 then printf,lun,'rbin_ar=',$
   strjoin(strtrim(string(rbin_ar,format='(F6.2)'),2),','),format='(A8,A100)'
printf,lun,'dmprof=',dmprof
printf,lun,'bmdl=',bmdl
printf,lun,'massfn=',massfn
printf,lun,'conc=',conc
printf,lun,'coll_2h=',coll_2h
printf,lun,'pimax=',strtrim(string(pimax),2)
if n_elements(diag) gt 0 then printf,lun,'diag=',diag
printf,lun,'xim_meth=',xim_meth
printf,lun,'inp_dmh_rt=',inp_dmh_rt

free_lun,lun


end


